package training;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_6_20_Training {
    //K 次修改后的最大曼哈顿距离
    public int maxDistance(String s, int k) {
        int[] map = new int[4];
        int max = 0;
        for (char c : s.toCharArray()) {
            if (c == 'N') {
                map[0]++;
            } else if (c == 'S') {
                map[1]++;
            } else if (c == 'E') {
                map[2]++;
            } else if (c == 'W') {
                map[3]++;
            }
            // N+W
            max = Math.max(max, map[0] + map[3] - map[1] - map[2] + 2 * Math.min(k, map[2] + map[1]));
            // N+E
            max = Math.max(max, map[0] + map[2] - map[1] - map[3] + 2 * Math.min(k, map[3] + map[1]));
            // S+W
            max = Math.max(max, map[1] + map[3] - map[0] - map[2] + 2 * Math.min(k, map[2] + map[0]));
            // S+E
            max = Math.max(max, map[1] + map[2] - map[0] - map[3] + 2 * Math.min(k, map[3] + map[0]));
        }
        return max;
    }
}
